package com.cys.dm.rest.service;

import com.cys.dm.client.base.DmImageClient;
import com.cys.dm.client.item.DmCinemaClient;
import com.cys.dm.client.item.DmItemClient;
import com.cys.dm.client.item.DmItemCommentClient;
import com.cys.dm.client.scheduler.DmSchedulerClient;
import com.cys.dm.client.scheduler.DmSchedulerSeatPriceClient;
import com.cys.dm.entity.item.DmCinema;
import com.cys.dm.entity.item.DmItem;
import com.cys.dm.entity.item.DmItemComment;
import com.cys.dm.entity.scheduler.DmScheduler;
import com.cys.dm.entity.scheduler.DmSchedulerSeatPrice;
import com.cys.dm.vo.dmitem.DmItemDetailResponse;
import com.cys.dm.vo.scheduler.DmSchedulerPicResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class DmItemDetailService {

    @Autowired
    private DmItemClient dmItemClient;
    @Autowired
    private DmCinemaClient dmCinemaClient;
    @Autowired
    private DmItemCommentClient dmItemCommentClient;
    @Autowired
    private DmImageClient dmImageClient;
    @Autowired
    private DmSchedulerClient dmSchedulerClient;
    @Autowired
    private DmSchedulerSeatPriceClient dmSchedulerSeatPriceClient;


    /**
     * 根据商品id返回商品详情页
     * @param id
     * @return
     */
    public DmItemDetailResponse queryItemDetail(Long id){
        //根据商品ID查出商品信息
        DmItem dmItem = dmItemClient.findByIdForItem(id);
        //剧院信息
        DmCinema dmCinema = dmCinemaClient.findById(dmItem.getCinemaId());

        DmItemDetailResponse itemDetail = new DmItemDetailResponse();
        itemDetail.setId(dmItem.getId());
        itemDetail.setAbstractMessage(dmItem.getAbstractMessage());
        itemDetail.setItemName(dmItem.getItemName());
        itemDetail.setItemType1Id(dmItem.getItemType1Id());
        itemDetail.setItemType1Name(dmItem.getItemType1Name());
        itemDetail.setItemType2Id(dmItem.getItemType2Id());
        itemDetail.setItemType2Name(dmItem.getItemType2Name());
        itemDetail.setAreaId(dmCinema.getAreaId());
        itemDetail.setAreaName(dmCinema.getAreaName());
        itemDetail.setStartTime(dmItem.getStartTime());
        itemDetail.setEndTime(dmItem.getEndTime());
        itemDetail.setState(dmItem.getState());
        itemDetail.setBasicDescription(dmItem.getBasicDescription());
        itemDetail.setProjectDescription(dmItem.getProjectDescription());
        itemDetail.setReminderDescription(dmItem.getReminderDescription());
        itemDetail.setImgUrl(dmImageClient.findItenPicByID(id).getImgUrl());
        itemDetail.setCinemaId(dmItem.getCinemaId());
        itemDetail.setAdress(dmCinema.getAddress());
        itemDetail.setLatitude(dmCinema.getLatitude());
        itemDetail.setLongitude(dmCinema.getLongitude());
        itemDetail.setAvgScore(dmItem.getAvgScore());
        itemDetail.setCommentCount(dmItem.getCommentCount());

        return itemDetail;
    }

    /**
     * 根据商品ID查询商品排期
     * @param itemId
     * @return
     */
    public List<DmScheduler> findSchedulerById(@RequestParam Long itemId){
        return dmSchedulerClient.findSchedulerById(itemId);
    };

    /**
     * 根据商品排期查询商品价格
     * @param scheduleId
     * @return
     */
    public List<DmSchedulerPicResponse> queryItemPrice(@RequestParam Long scheduleId){
        List<DmSchedulerSeatPrice> dmSchedulerSeatPrices = dmSchedulerSeatPriceClient.queryItemPrice(scheduleId);
        List<DmSchedulerPicResponse> dmSchedulerPicResponseList = new ArrayList<>();
        dmSchedulerSeatPrices.forEach(dmSchedulerSeatPrice->{
            DmSchedulerPicResponse dmSchedulerPicResponse = new DmSchedulerPicResponse();
            dmSchedulerPicResponse.setId(dmSchedulerSeatPrice.getId());
            dmSchedulerPicResponse.setPrice(dmSchedulerSeatPrice.getPrice());
            dmSchedulerPicResponse.setScheduleId(dmSchedulerSeatPrice.getScheduleId());
            if (dmSchedulerSeatPrice.getSeatNum()<=0){
                dmSchedulerPicResponse.setIsHaveSeat(0);
            }else {
                dmSchedulerPicResponse.setIsHaveSeat(1);
            }
            dmSchedulerPicResponseList.add(dmSchedulerPicResponse);
        });
        return dmSchedulerPicResponseList;
    };

    /**
     *根据根据戏剧 获取评价信息及数据信息
     * @param id
     * @return
     */
    public List<DmItemComment> findAllByItemIdComment(@RequestParam Long id){
        return dmItemCommentClient.findAllByItemId(id);
    }

    /**
     * 提交评价信息
     * @param params
     * @return
     */
    public int commitItemComment(@RequestParam Map<String,Object> params){
        return dmItemCommentClient.commitItemComment(params);
    }

    

}
